<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css">
    <style>
        .layui-upload-img {
            width: 92px;
            height: 92px;
            margin: 0 10px 10px 0;
        }
    </style>

</head>
<body>
    <button type="button" class="layui-btn" id="test1">
        <i class="layui-icon">&#xe67c;</i>上传图片
    </button>
    <div class="layui-upload-list">
        <img class="layui-upload-img" id="demo1">
        <p id="demoText"></p>
    </div>
<form class="layui-form" action="">

    <div class="layui-form-item">
        <label class="layui-form-label">照片名称</label>
        <div class="layui-input-block">
            <input type="text" id="pName" name="PName" lay-verify="title" autocomplete="off" placeholder="请输入姓名"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">照片路径</label>
        <div class="layui-input-block">
            <input type="text" id="pPath" name="PPath" lay-verify="title" autocomplete="off" placeholder="请输入姓名"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">照片作者</label>
        <div class="layui-input-block">
            <input type="text" id="eName" name="EName" lay-verify="title" autocomplete="off" placeholder="请输入姓名"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="../../assets/libs/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="../../assets/js/my.js"></script>
<script>
    layui.use(['form', 'upload', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery
            , upload = layui.upload;

        upload.render({
            elem: '#test1' //绑定元素
            , url:host+'/photoservlet/upload' //上传接口
            , done: function (res) {
                if (res.code == 0) {
                    layer.msg('上传成功', {icon: 1})
                    $('#pPath').val(res.path)
                }
            }
            , error: function () {
                //请求异常回调
            }
        })

        // form.verify({
        //     title: function(value){
        //         if(value.length < 5){
        //             return '标题至少得5个字符啊';
        //         }
        //     }
        //
        //     ,content: function(value){
        //         layedit.sync(editIndex);
        //     }
        // });

        //日期

        form.on('submit(demo1)', function (data) {
            layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })
            $.get(host + '/photoservlet/save', data.field, function (r) {
                if (r.code == 0) {
                    layer.msg("添加成功", {icon: 1})
                } else {
                    layer.msg("添加失败", {icon: 2})
                }
            })
        });
    })
</script>
</html>